package cn.woflcode.game.view;

import cn.woflcode.game.Global.Global;
import cn.woflcode.game.model.*;
import cn.woflcode.game.model.util.AudioPlayer;
import cn.woflcode.game.model.util.HitUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.MemoryImageSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
//画图，绘图功能，输入paintComponent 选中回车给 g.(画板工具)
 /*  //画一条横线 四个参数 起点 终点的坐标（x,y）
        g.drawLine(100,100,200,200);
        //画一个长方形  提供起点（x,y），还有宽高
        g.drawRect(300,300,150,100);
        //画一个实心（fill）的长方形  提供起点（x,y），还有宽高
        g.fillRect(300,100,100,100);
        //设置画笔颜色
        g.setColor(Color.RED);
        //画实心圆  提供圆心（x,y）,两条直径 相当于宽高 椭圆还是圆
        g.fillOval(10,200,100,100);*/
//设置背景图

/**
 *
 * 游戏的画板，游戏所以的图案，图像都是通过画板呈现的
 */
public class Gamepanel extends JPanel {
    private List<Life> lifes;
   // private Life life;
    //创建游戏背景对象
    private Backgroud backgroud;
    //创建英雄战机的对象
    private HeroPlane heroPlane;
    //创建英雄战机的子弹对象
    private List<HeroBullet> heroBullets;
    //创建小敌机对象
    private List<SmallEnemyPlane> smallEnemyPlanes;
    //创建大敌机对象
    private List<BigEnemyPlane> bigEnemyPlanes;
    //创建小敌机子弹群对象
    private  List<EnemyBullet> enemyBullets;
    //创建大敌机子弹群对象
    private  List<BigEnemyBullet> bigEnemyBullets;
    //创建播放音乐对象
    private AudioPlayer audioPlayer =new AudioPlayer("assets/audio/bgm.mp3",true);
    //private AudioPlayer audioPlayer1 =new AudioPlayer("assets/audio/bullet.mp3",true);
    //初始化操作时构造器在做
    public Gamepanel(){
        this.init();
    }
    //初始化方法
    private void init(){
        lifes = new ArrayList<>();
        //life=new Life();
        backgroud = new Backgroud();
        heroPlane= new HeroPlane();
        heroBullets=new ArrayList<>();
        smallEnemyPlanes=new ArrayList<>();
        bigEnemyPlanes=new ArrayList<>();
        enemyBullets=new ArrayList<>();
        bigEnemyBullets=new ArrayList<>();
    }



    //画图,绘制功能 ,只要输入paintComponent 即可显示,选中回车就行
    protected void paintComponent(Graphics g) {
        //设置背景图

        backgroud.drawSelf(g);
        //设置英雄战机
        //只要当前游戏态是状运行状态，才显示英雄战机
        if(Global.currentstate==Global.GameState.RUNNING){
            /*if(heroPlane.addScore(int score){

            }*/

            heroPlane.drawSelf(g);
            //画出英雄战机的子弹
            for (HeroBullet heroBullet : heroBullets) {
                heroBullet.drawSelf(g);
            }
             //画出小敌机群 一堆小敌机 遍历出来
            for (SmallEnemyPlane smallEnemyPlane : smallEnemyPlanes) {
                smallEnemyPlane.drawSelf(g);
            }
            //画出大敌机群 一堆小敌机 遍历出来
            for (BigEnemyPlane bigEnemyPlane : bigEnemyPlanes) {
                bigEnemyPlane.drawSelf(g);
            }
            //小敌机子弹
            for (EnemyBullet enemyBullet : enemyBullets){
                enemyBullet.drawSelf(g);
            }

            //大敌机子弹
            for (BigEnemyBullet bigenemyBullet : bigEnemyBullets) {
                bigenemyBullet.drawSelf(g);
            }
            //life.drawSelf(g);
            for (Life life:lifes){
                life.drawSelf(g);
            }
        }
    }
    //碰撞检测方法
    public void check(){
        //英雄战机和鸡腿
        for (int i = 0; i < lifes.size(); i++) {
            Life life = lifes.get(i);
            if(HitUtil.heroPlaneVSLife(heroPlane,life)){
                heroPlane.addlife(1);
                lifes.remove(i);
                break;
            }

        }
        //英雄战机和小敌机
        for (int i = 0; i < smallEnemyPlanes.size(); i++) {
            SmallEnemyPlane enemyPlane = smallEnemyPlanes.get(i);
            //如果敌机是  存活状态的时候发生碰撞
            if (enemyPlane.getState()== Global.FlyObjectState.ALIVE
                    && HitUtil.heroPlaneVSEnemyPlane(heroPlane,enemyPlane)){
                //敌机直接死亡
                enemyPlane.setState(Global.FlyObjectState.DEATH);
                new AudioPlayer("assets/audio/boom.mp3",false).start();
                //英雄战机生命数-1
                heroPlane.attacked(1);
                //判断,如果英雄战机没有了生命数 --> 游戏结束
                if (!heroPlane.isAlive()){
                    Global.currentstate= Global.GameState.STOP;
                    //关闭音乐
                    audioPlayer.close();
                    //audioPlayer1.close();
                }
                break;
            }
        }
        //英雄战机和大敌机
        for (int i = 0; i < bigEnemyPlanes.size(); i++) {
            BigEnemyPlane bigenemyPlane = bigEnemyPlanes.get(i);
            //如果敌机是  存活状态的时候发生碰撞
            if (bigenemyPlane.getState()== Global.FlyObjectState.ALIVE
                    && HitUtil.heroPlaneVSBigEnemyPlane(heroPlane,bigenemyPlane)){
                //敌机直接死亡
                bigenemyPlane.setState(Global.FlyObjectState.DEATH);
                new AudioPlayer("assets/audio/boom.mp3",false).start();
                //英雄战机生命数-1
                heroPlane.attacked(1);
                //判断,如果英雄战机没有了生命数 --> 游戏结束
                if (!heroPlane.isAlive()){
                    Global.currentstate= Global.GameState.STOP;
                    //关闭音乐
                    audioPlayer.close();
                    //audioPlayer1.close();
                }
                break;
            }
        }

        //英雄战机和小敌机子弹
      for (int i = 0; i <enemyBullets.size(); i++) {
            //把敌机子弹取出来
            EnemyBullet enemyBullet = enemyBullets.get(i);
            //判断当前子弹的状态,此时发现没有getter和setter方法
            if (enemyBullet.getState()== Global.FlyObjectState.ALIVE
                    && HitUtil.heroPlaneVSEnemyBullet(heroPlane,enemyBullet)){
                //敌机子弹状态,直接从ALIVE --> DELETABLE
                enemyBullet.setState(Global.FlyObjectState.DELETABLE);
                //英雄战机的生命数 -1
                heroPlane.attacked(1);
                //判断,如果英雄战机没有生命数 --> 游戏结束
                if (!heroPlane.isAlive()){
                    Global.currentstate= Global.GameState.STOP;
                    audioPlayer.close();
                    HeroPlane.totalScore = 0;
                    //audioPlayer1.close();
                }
                break;
            }
        }
        //英雄战机和大敌机子弹
        for (int i = 0; i <bigEnemyBullets.size(); i++) {
            //把敌机子弹取出来
            BigEnemyBullet bigenemyBullet = bigEnemyBullets.get(i);
            //判断当前子弹的状态,此时发现没有getter和setter方法
            if (bigenemyBullet.getState()== Global.FlyObjectState.ALIVE
                    && HitUtil.cc(heroPlane,bigenemyBullet)){
                //敌机子弹状态,直接从ALIVE --> DELETABLE
                bigenemyBullet.setState(Global.FlyObjectState.DELETABLE);
                //英雄战机的生命数 -1
                heroPlane.attacked(1);
                //判断,如果英雄战机没有生命数 --> 游戏结束
                if (!heroPlane.isAlive()){
                    Global.currentstate= Global.GameState.STOP;
                    audioPlayer.close();
                    //audioPlayer1.close();
                }
                break;
            }
        }
        //英雄子弹和小敌机
        for (int i = 0; i < heroBullets.size(); i++) {
            HeroBullet heroBullet = heroBullets.get(i);
            for (int j = 0; j <smallEnemyPlanes.size() ; j++) {
                SmallEnemyPlane smallEnemyPlane = smallEnemyPlanes.get(j);
                //英雄子弹、敌机状态是否是存活，英雄子弹和敌机是否碰撞
                if (heroBullet.getState()==Global.FlyObjectState.ALIVE
                        && smallEnemyPlane.getState()==Global.FlyObjectState.ALIVE
                        &&HitUtil.heroBulletVSEnemyPlane(heroBullet,smallEnemyPlane)){
                    heroBullet.setState(Global.FlyObjectState.DELETABLE);
                    new AudioPlayer("assets/audio/boom.mp3",false).start();
                    smallEnemyPlane.attacked(1);
                    if(!smallEnemyPlane.isAlive()){
                        smallEnemyPlane.setState(Global.FlyObjectState.DEATH);
                    }
                }
            }
        }
        //英雄子弹和大敌机
      for (int i = 0; i < heroBullets.size(); i++) {
            HeroBullet heroBullet = heroBullets.get(i);
            for (int j = 0; j <bigEnemyPlanes.size() ; j++) {
                BigEnemyPlane bigEnemyPlane = bigEnemyPlanes.get(j);
                //英雄子弹、敌机状态是否是存活，英雄子弹和敌机是否碰撞
                if (heroBullet.getState()==Global.FlyObjectState.ALIVE
                        && bigEnemyPlane.getState()==Global.FlyObjectState.ALIVE
                        &&HitUtil.aa(heroBullet,bigEnemyPlane)){
                    heroBullet.setState(Global.FlyObjectState.DELETABLE);
                    new AudioPlayer("assets/audio/boom.mp3",false).start();
                    bigEnemyPlane.attacked(1);
                    if(!bigEnemyPlane.isAlive()){
                        bigEnemyPlane.setState(Global.FlyObjectState.DEATH);
                    }
                }
            }
        }
        //英雄子弹和小敌机子弹
        for (int i = 0; i < heroBullets.size(); i++) {
            HeroBullet heroBullet = heroBullets.get(i);
            for (int j = 0; j < enemyBullets.size(); j++) {
                EnemyBullet enemyBullet = enemyBullets.get(j);
                //判断,英雄子弹和敌机子弹都是ALIVE,发生碰撞
                if (heroBullet.getState()== Global.FlyObjectState.ALIVE
                        && enemyBullet.getState()== Global.FlyObjectState.ALIVE
                        && HitUtil.heroBulletVSEnemyBullet(heroBullet,enemyBullet)){
                    //英雄子弹和敌机子弹
                    heroBullet.setState(Global.FlyObjectState.DELETABLE);
                    enemyBullet.setState(Global.FlyObjectState.DELETABLE);
                    break;
                }
            }
        }
        //英雄子弹和大敌机子弹
        for (int i = 0; i < heroBullets.size(); i++) {
            HeroBullet heroBullet = heroBullets.get(i);
            for (int j = 0; j < bigEnemyBullets.size(); j++) {
                BigEnemyBullet bigEnemyBullet = bigEnemyBullets.get(j);
                //判断,英雄子弹和敌机子弹都是ALIVE,发生碰撞
                if (heroBullet.getState()== Global.FlyObjectState.ALIVE
                        && bigEnemyBullet.getState()== Global.FlyObjectState.ALIVE
                        && HitUtil.hearBulletVSBigEnemyBullet(heroBullet,bigEnemyBullet)){
                    //英雄子弹和敌机子弹
                    heroBullet.setState(Global.FlyObjectState.DELETABLE);
                    bigEnemyBullet.setState(Global.FlyObjectState.DELETABLE);
                    break;
                }
            }
        }

    }

    //清理战场/已发生碰撞的飞机/已经发生碰撞的的飞行物
    public void clear(){
        //英雄战机子弹，遍历操作 一边遍历一边判断是否删除
        for (int i = 0; i < heroBullets.size(); i++) {
            //根据
            HeroBullet heroBullet = heroBullets.get(i);
            if(heroBullet.outOfBound()|| heroBullet.getState()== Global.FlyObjectState.DELETABLE){
                //说明越界了
                heroBullets.remove(i);
                break;
            }
        }
        //清理小敌机越界
        for (int i = 0; i < smallEnemyPlanes.size(); i++) {
            SmallEnemyPlane smallEnemyPlane = smallEnemyPlanes.get(i);
            if(smallEnemyPlane.outOfBound()) {
                smallEnemyPlanes.remove(i);
            }
            //状态是可删除状态的敌机
            if(smallEnemyPlane.getState()==Global.FlyObjectState.DELETABLE){
                //敌机爆炸，英雄战机得分
                smallEnemyPlanes.remove(i);
                heroPlane.addScore(smallEnemyPlane.getScore());
                break;
            }
        }
        //大敌机越界
        for (int i = 0; i <bigEnemyPlanes.size() ; i++) {
            BigEnemyPlane bigEnemyPlane = bigEnemyPlanes.get(i);
            if(bigEnemyPlane.outOfBound()||bigEnemyPlane.getState()== Global.FlyObjectState.DELETABLE){

                    bigEnemyPlanes.remove(i);
                    heroPlane.addScore(bigEnemyPlane.getScore());
                    break;

            }
        }
        //清理敌机子弹越界  或者是状态可删除的子弹
        for (int i = 0; i < enemyBullets.size(); i++) {
            EnemyBullet enemyBullet = enemyBullets.get(i);
            if (enemyBullet.outOfBound()||enemyBullet.getState()==Global.FlyObjectState.DELETABLE){
                 enemyBullets.remove(i);
                 break;
            }
        }
        //清理大敌机子弹越界  或者是状态可删除的子弹
        for (int i = 0; i < bigEnemyBullets.size(); i++) {
            BigEnemyBullet bigenemyBullet = bigEnemyBullets.get(i);
            if (bigenemyBullet.outOfBound()||bigenemyBullet.getState()==Global.FlyObjectState.DELETABLE){
                bigEnemyBullets.remove(i);
                break;
            }
        }
    }
    //设置监听器
    public void  listener(){
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //创建鼠标的适配器对象，作为鼠标事件的参数
            //鼠标点击
            public void mouseClicked(MouseEvent e) {
                //准备->运行
                 if(Global.currentstate == Global.GameState.READY){
                     Global.currentstate=Global.GameState.RUNNING;
                     //启动线程
                     audioPlayer. start();
                     //audioPlayer1. start();
                 }
                 //结束->准备
                if(Global.currentstate == Global.GameState.STOP){
                    HeroPlane.totalScore = 0;
                    Global.currentstate=Global.GameState.READY;
                    //游戏重新开始，初始化
                    init();
                    audioPlayer = new AudioPlayer("assets/audio/bgm.mp3",true);
                    //audioPlayer1 = new AudioPlayer("assets/audio/bullet.mp3",true);
                }
            }
            //鼠标进入
            public void mouseEntered(MouseEvent e) {
                //
                if(Global.currentstate==Global.GameState.PAUSE){
                    Global.currentstate=Global.GameState.RUNNING;
                    //重新启动线程,注意判断:如果当前的音乐线程已死,那么只能重新创建线程,再次让线程启动
                    if (!audioPlayer.isAlive()){
                        audioPlayer = new AudioPlayer("assets/audio/bgm.mp3",true);
                        //audioPlayer1 = new AudioPlayer("assets/audio/bullet.mp3",true);
                        audioPlayer.start();
                       // audioPlayer1.start();
                    }
                }
            }
            //鼠标离开
            public void mouseExited(MouseEvent e) {
                if(Global.currentstate==Global.GameState.RUNNING){
                    Global.currentstate=Global.GameState.PAUSE;
                    //线程关闭
                    audioPlayer.close();
                    //audioPlayer1.close();
                }
            }
            //鼠标移动 再running状态时  让英雄战机跟随我们鼠标移动
            public void mouseMoved(MouseEvent e) {
                heroPlane.move(e.getX(),e.getY());
            }
        };
        //鼠标事件的注册适配器和事件处理代码，注册鼠标移动事件
        this.addMouseMotionListener(mouseAdapter);
        //注册鼠标点击事件
        this.addMouseListener(mouseAdapter);
            Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(0, 0, new int[0], 0, 0));
            setCursor(Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), null));
        }

    //定义计时器
    private int count=0;
    //定时器方法
    public void timer() {
        //java.util包
        Timer timer = new Timer();
        //调用timer对象的调度
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(Global.currentstate==Global.GameState.RUNNING) {
                    count++;
                    //count%50==0,500ms发射一次
                    HeroPlane heroPlane = new HeroPlane();


                    if (count % 1000 == 0) {
                        lifes.add(new Life());
                    }

                    if(count %80==0){
                        for (BigEnemyPlane bigEnemyPlane : bigEnemyPlanes){
                            bigEnemyPlane.fire();
                            bigEnemyBullets.addAll(bigEnemyPlane.fire());
                        }
                    }
                    if(count%80==0){
                        //小敌机开火
                        for (SmallEnemyPlane smallEnemyPlane : smallEnemyPlanes){
                            smallEnemyPlane.fire();
                            enemyBullets.addAll(smallEnemyPlane.fire());
                        }


                    }
                    /*if(count%10==0) {
                        life.move();
                        life.move();
                    }*/
                    if (count %10 == 0) {
                        //英雄战机开火
                        Gamepanel.this.heroPlane.fire();
                        heroBullets.addAll(Gamepanel.this.heroPlane.fire());

                    }

                    //让英雄战机子弹移动
                    for (HeroBullet heroBullet : heroBullets) {
                        heroBullet.move();
                    }
                    if(count%100==0){
                        smallEnemyPlanes.add(new SmallEnemyPlane());
                        //bossPlanes.add(new BossPlane());
                        //System.out.println("英雄战机子弹数: "+ heroBullets.size());
                        //System.out.println("小敌机数: "+ smallEnemyPlanes.size());
                       // System.out.println("大敌机子弹数: "+ bigEnemyBullets.size());
                    }
                    if(count%150==0){
                        bigEnemyPlanes.add(new BigEnemyPlane() );//大敌机
                    }

                    for (SmallEnemyPlane smallEnemyPlane : smallEnemyPlanes){
                        smallEnemyPlane.move();
                    }
                    //让小敌机动起来
                    for (SmallEnemyPlane smallEnemyPlane : smallEnemyPlanes) {
                        smallEnemyPlane.move();
                    }
                    for (BigEnemyPlane bigEnemyPlane : bigEnemyPlanes) {
                        bigEnemyPlane.move();
                    }
                    for (EnemyBullet enemyBullet : enemyBullets) {
                        enemyBullet.move();
                    }
                    //让大敌机子弹动起来
                    for (BigEnemyBullet bigEnemyBullet:bigEnemyBullets){
                        bigEnemyBullet.move();
                    }
                    for (Life life:lifes){
                        life.move();
                    }

                }
                check();
                clear();
                    repaint();
            }

        },10,10);
    }
}
